#include "videostreamimpl.h"
#include "synchronousav.h"
#include "videodecodetask.h"
VideoStreamImpl::VideoStreamImpl(AVStream *stream, MediaImagehandler *handler):
    StreamAbstract(stream),
    m_oMediaImagehandler(handler)
{
    bInit = false;
    m_oVideoDecodeTask  = new VideoDecodeTask;
    m_oSynchronousAV    = new SynchronousAV;
}

VideoStreamImpl::~VideoStreamImpl()
{
    delete m_oVideoDecodeTask;
    delete m_oSynchronousAV;
}

void VideoStreamImpl::SetTimerSynchTarget(TimerSynchAbstract *Target)
{
    m_oSynchTarget = Target;
}

bool VideoStreamImpl::is_init()
{
    return bInit;
}

bool VideoStreamImpl::Init()
{
    bInit = true;
    if(m_oSynchronousAV)
    {
        m_oSynchronousAV->init(m_oMediaImagehandler,m_oSynchTarget);
        m_oSynchronousAV->activate();
    }

    if(m_oVideoDecodeTask)
    {
        if(m_oVideoDecodeTask->Init(GetStream(),m_oSynchronousAV) == false)
            return false;
        m_oVideoDecodeTask->activate();
    }
    return true;
}

bool VideoStreamImpl::UnInit()
{
    if(m_oVideoDecodeTask)
    {
        m_oVideoDecodeTask->close();
        m_oVideoDecodeTask->UnInit();
    }

   if(m_oSynchronousAV)
   {
       m_oSynchronousAV->close();
   }
   return true;
}

bool VideoStreamImpl::receive_handler(const char *data, int size)
{
    if(m_oVideoDecodeTask)
        m_oVideoDecodeTask->receive_handler(data,size);
    return true;
}
